package lvbu.wang.lvbuoperator.common;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import lvbu.wang.lvbuoperator.bean.JsonMotor;
import lvbu.wang.lvbuoperator.http.IotHttpHelp;
import lvbu.wang.lvbuoperator.http.IotHttpHelpWS;

public class MotorHelper {
	private static String tag = "myLog-MotorHelper";

	/**
	 * 判断蓝牙地址是否正确
	 *
	 * @param bluetoothAddress
	 * @return
	 */
	public static Boolean checkBluetoothAddress(String bluetoothAddress) {
		if (bluetoothAddress.length() != 17) {
			return false;
		}
		return true;
	}

	/**
	 * 通过http发送命令
	 * @param motorId
	 * @param sendCode
	 * @param sendContent
	 * @param context
     * @return
     */
	public static String sendCommand_http(Long motorId, String sendCode, String sendContent, Context context) { // 通过http发送命令

		if (sendContent.length() < 18) {// sendContent长度不够时，不允许执行
			return "sendContent错误";
		}
		String id = IotHttpHelpWS.makeMotorCommandAddressId(motorId, sendCode);
		Map<String, Object> queryStrings = new HashMap<String, Object>();
		queryStrings.put("id", id);
		queryStrings.put("motorId", motorId);
		queryStrings.put("sendCode", sendCode);
		queryStrings.put("sendContent", sendContent);
		String source = IotHttpHelpWS.getSource("api/motor/Set_motorCommandAndAnswer", queryStrings, true, context);
		Log.e("mainVBNMJKL","source = " + source);
		if (IotHttpHelp.checkSource(source) == true) {
			JsonMotor.MotorCommand motorCommand = new Gson().fromJson(source, JsonMotor.MotorCommand.class);
			if (motorCommand.getAnswerResult().equals("02")) {// 收到应答
				String answerContent = motorCommand.getAnswerContent();
				if (answerContent.subSequence(18, 20).equals("01")) {
					return "";
				} else {
					return "电机应答结果：设置失败！";
				}
			} else {	// 应答超时或者其他
				return "电机应答结果：" + motorCommand.getAnswerResult() + "";
			}
		} else {
			return IotHttpHelp.source2Message(context, source);
		}

	}

	public static String sendCommand_http_(Long motorId, String sendCode, String sendContent, Context context) { // 通过http发送命令
		if (sendContent.length() < 18) {// sendContent长度不够时，不允许执行
			return "sendContent错误";
		}
		String id = IotHttpHelpWS.makeMotorCommandAddressId(motorId, sendCode);
		Map<String, Object> queryStrings = new HashMap<String, Object>();
		queryStrings.put("id", id);
		queryStrings.put("motorId", motorId);
		queryStrings.put("sendCode", sendCode);
		queryStrings.put("sendContent", sendContent);
		String source = IotHttpHelpWS.getSource("api/motor/Set_motorCommandAndAnswer", queryStrings, true, context);
		if (IotHttpHelpWS.checkSource(source) == true) {
			JsonMotor.MotorCommand motorCommand = new Gson().fromJson(source, JsonMotor.MotorCommand.class);
			if (motorCommand.getAnswerResult().equals("02")) {// 收到应答
				String answerContent = motorCommand.getAnswerContent();
				if (answerContent.subSequence(18, 20).equals("01")) {
					return answerContent;
				} else {
					return "电机应答结果：设置失败！";
				}
			} else {// 应答超时或者其他
				return "电机应答结果：" + motorCommand.getAnswerResult() + "";
			}
		} else {
			return IotHttpHelp.source2Message(context, source);
		}

	}

	public static String getMotorStatusName(String statusCode, Context context) {
		Map<String, Object> queryStrings = new HashMap<String, Object>();
		queryStrings.put("statusCode", statusCode);
		final String source = IotHttpHelp.getData("api/motor/Get_JsonMotor_MotorStatusDict_byStatusCode", queryStrings, true, context);
		if (IotHttpHelp.checkSource(source) == true) {
			final JsonMotor.MotorStatusDict motorStatusDict = new Gson().fromJson(source, JsonMotor.MotorStatusDict.class);
			return motorStatusDict.getStatusName();
		}
		return "";

	}

}
